<?php

function getElements($parentId) 
{
	$db = getMysqlObject();	
	$date = getWorkDay();
	
	if($parentId==2) {
		$sql = "SELECT Name, pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, pharmacyURL, date
	                   FROM  Counties
	                   WHERE  Counties.parentId = $parentId 
	                   ORDER BY Name
	                   LIMIT 0 , 500";
	}
	else {
		$sql = "SELECT Name, pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, pharmacyURL, date
	                   FROM  Counties
	                   WHERE  Counties.parentId = $parentId 
	                   LIMIT 0 , 500";
	}

    $result = $db->query($sql);

    if ($result !== false)
    {
        $elementsArray = array(); 
        while ($row = $db->fetchObject($result))
		{
            if($row->isPharmacy == 0) {
                array_push($elementsArray, getLocationInfo($row));
            }
            else {
            	if( strcmp($date,$row->date ) == 0 ) {
            		array_push($elementsArray, getPharmacyInfo($row));
            	}
            }
        }
    } 
    return $elementsArray;
}

function search($countyName, $parentId)
{
	$db = getMysqlObject();
	$date = getWorkDay();
	
    $strTemp = $countyName;
    $strTemp = trim($strTemp);
    $parentIdTemp = $parentId;
     
	$words = explode(" ",$countyName); 
	for($i=0;$i<count($words);$i++)
	{
		$query_string .= "(Counties.Name LIKE '%".$words[$i]."%' OR Counties.pharmacyAddress LIKE '%".$words[$i]."%') AND ";
	}
	$query_string = substr($query_string,0,strlen($query_string)-4);
     
    if($parentId == 0) {
		$sql = "SELECT Name, parentId,pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, date
					FROM  Counties 
					WHERE  ".$query_string." AND Counties.isPharmacy !=0 AND Counties.date = '$date'
					ORDER BY Name
					LIMIT 0 , 100";
    }
    else if($parentId == 2) {
    	$sql = "SELECT Name, parentId, pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, date
					FROM  Counties 
					WHERE  ".$query_string." AND Counties.isPharmacy !=0 AND Counties.parentId != 1 AND Counties.parentId != 3 AND Counties.date = '$date'
					ORDER BY Name
					LIMIT 0 , 100";
    }
    else {
        $sql = "SELECT Name, parentId, pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, date
					FROM  Counties 
					WHERE  ".$query_string." AND Counties.isPharmacy !=0 AND Counties.parentId = $parentId  AND Counties.date = '$date'
					ORDER BY Name
					LIMIT 0 , 100";
    }
    $result = $db->query($sql);
    
    if ( $result !== false )
    {
        $pharmaciesArray = array();
        while ($row = $db->fetchObject($result)) {
            array_push($pharmaciesArray, getPharmacyInfo($row));
        }
    } 
    return $pharmaciesArray;
}

function searchPlace($lattitude, $longitude, $range)
{
	$db = getMysqlObject();
	$date = getWorkDay();
	
	
	$sql="SELECT Name, pharmacyAddress, pharmacyPhone, ID, isPharmacy, lattitude, longitude, date,
			( 6371 * ACOS( COS( RADIANS( $lattitude ) ) * 
			COS( RADIANS( Counties.lattitude ) ) * 
			COS( RADIANS( Counties.longitude ) - RADIANS( $longitude ) ) + 
			SIN( RADIANS( $lattitude ) ) * SIN( RADIANS( Counties.lattitude ) ) ) ) 
			AS distance
			FROM Counties
			WHERE isPharmacy =1 AND Counties.date = '$date'
			HAVING distance < $range 
			ORDER BY distance
			LIMIT 0 , 100";
   
    $result = $db->query($sql);
    
    if ( $result !== false )
    {
        $pharmaciesArray = array();
        while ($row = $db->fetchObject($result)) {
            array_push($pharmaciesArray, getPharmacyInfo($row));
        }
    }
    
    return $pharmaciesArray;
}

function getPharmacyInfo($row)
{
	$pharmacy = array();
	$pharmacy = array('Name' => $row->Name, 
					'Address' => $row->pharmacyAddress,
					'Phone' => $row->pharmacyPhone, 
  					'isPharmacy' => $row->isPharmacy,
					'id' => $row->ID ,
    				'lattitude' => $row->lattitude,
        			'longitude' => $row->longitude,
					'URL' => $row->pharmacyURL);
	return $pharmacy;
}

function getLocationInfo($row)
{
	$location = array();
	$location = array( 'Name' => $row->Name,
						'isPharmacy' => $row->isPharmacy,
						'id' => $row->ID,
						'lattitude' => $row->lattitude,
        				'longitude' => $row->longitude);
	return $location;
}

function getMysqlObject()
{
	require_once('Mysql.php');
	require_once('config.php');
	$db = new MySQL($dbHost, $dbUsername, $dbPassword, $dbName);	
    return $db;
}

function getWorkDay()
{
	date_default_timezone_set("Europe/Minsk"); //proper timezone for Turkey
	$currentHour = date('h', time());
	$currentA = date('a', time());
	$date = date('Y-m-d',mktime(0,0,0,date('m'), date('d'), date('Y')));
	
	if($currentA == 'am' && ($currentHour < 9 || $currentHour == 12))
		$date = date('Y-m-d',mktime(0,0,0,date('m'), date('d')-1, date('Y')));
	
	return $date;
}
?>
